package com.bojx.report.util;

public class ExcelUtil {

	/***
	 * excel 列号转换为数字
	 * 
	 * @param columnName
	 * @return
	 */
	public static int excelColStrToNum(String colStr) {
		int num = 0;
		int result = 0;
		for (int i = 0; i < colStr.length(); i++) {
			char ch = colStr.charAt(colStr.length() - i - 1);
			num = (int) (ch - 'A' + 1);
			num *= Math.pow(26, i);
			result += num;
		}
		return result;
	}

	/***
	 * excel 数字转列号
	 * 
	 * @param columnIndex
	 * @return
	 */
	public static String excelColIndexToStr(int columnIndex) {
		if (columnIndex <= 0) {
			return null;
		}
		String columnStr = "";
		columnIndex--;
		do {
			if (columnStr.length() > 0) {
				columnIndex--;
			}
			columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
			columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
		} while (columnIndex > 0);
		return columnStr;
	}

	public static void main(String[] args) {

		System.out.println(excelColStrToNum("BK"));
	}

}
